package com.AnywheeBt;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.view.WindowManager;

public class BtCallList extends Activity {
	private static final String TAG = "BtCallList";	
	private ListView mCallListView = null;
	private MyAdapter mCallAdapter = null;	
//	private Button imgDelBtn = null;
//	private Button mBackBtn = null;
	private Button mContactBtn = null;
	private Button mDailBtn = null;
	
	private Button mMusicBtn = null;
	private Button mKeyboardBtn = null;
	
	private PbCallList call_list = new PbCallList();    
	
	public void DelDialog( )	{ 
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(getResources().getString(R.string.dlg_del_call_title));
		builder.setMessage(getResources().getString(R.string.dlg_del_call_msg));							
		builder.setNegativeButton(R.string.dlg_yes,new DialogInterface.OnClickListener() {			
			public void onClick(DialogInterface dialog, int which) {						
				call_list.DeleteListFile(PbCallList.BT_CALL_LIST);
				finish();
				//dialog.dismiss();		
			}			
		});
		
		builder.setPositiveButton(R.string.dlg_no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {				
				dialog.dismiss();
			}
		});
		
		builder.create().show();
	} 

    private BroadcastReceiver closeThisActivity = new BroadcastReceiver(){

		@Override
		public void onReceive(Context arg0, Intent arg1) {

			if((arg1.getAction()).equals("com.anywheetbt.btcalllist.close")){
				BtCallList.this.finish();
			}
		}
    	
    };
    
	@Override
    public void onCreate(Bundle savedInstanceState)   {
        super.onCreate(savedInstanceState); 
        Log.i(TAG,"onCreate");
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);        
        setContentView(R.layout.callrecord);
        call_list.get_list(PbCallList.BT_CALL_LIST);
        mCallListView = (ListView)this.findViewById(R.id.call_list_view);
		mCallAdapter = new MyAdapter(getBaseContext(),CallListData(),R.layout.call_list_item);					
		mCallListView.setAdapter(mCallAdapter);
		
//		imgDelBtn = (Button)this.findViewById(R.id.call_list_del);
//		mBackBtn = (Button)this.findViewById(R.id.call_list_back);
		mDailBtn = (Button)this.findViewById(R.id.call_list_dail);
		mContactBtn = (Button)this.findViewById(R.id.call_list_contact);
		mMusicBtn = (Button)this.findViewById(R.id.call_list_music);
		mKeyboardBtn = (Button)this.findViewById(R.id.call_list_keyboard);
		
//		mBackBtn.setOnClickListener(new OnClickListener(){
//			@Override
//			public void onClick(View v) {
//				onBackPressed();				
//			}			
//		});
//		
//		imgDelBtn.setOnClickListener(new OnClickListener(){
//			@Override
//			public void onClick(View v) {
//				DelDialog();				
//			}			
//		});
		
		mMusicBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BtCallList.this, BtMusic.class);
				startActivity(intent);
			}			
		});
	
		mKeyboardBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
			}			
		});
		
		mDailBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {			
			}			
		});		
		
		mContactBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();  
                intent.setClass(BtCallList.this, PbList.class);  
                startActivity(intent);
			}			
		});
		
		mCallListView.setOnItemClickListener( new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				mCallAdapter.setSelectItem(arg2);    
				TextView tv=(TextView)arg1.findViewById(R.id.call_list_text);
				String strBtNum=(String)tv.getText();
				Log.e(TAG,""+strBtNum);
				if(strBtNum !="" )  SzhBAction.CallPhone(strBtNum);					
			}			
		});
		
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.anywheetbt.btcalllist.close");
		registerReceiver(closeThisActivity, filter);
    }
	
	//Callist 
	public  ArrayList<HashMap<String, Object>> CallListData() {
		ArrayList<HashMap<String, Object>> mListtem = new ArrayList<HashMap<String, Object>>();		
		int nTotal = call_list.GetListSize();
		//
		for (int i = 0; i < nTotal; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			String strInout = call_list.GetListInOut(i);
			//
//			if( strInout.equals("true")){
//				map.put("item_image", R.drawable.bt_out_num);
//			}else{
//				map.put("item_image", R.drawable.bt_in_num);
//			}							
			map.put("call_list_text",call_list.GetListNum(i));
			map.put("call_list_name", call_list.GetListName(i));
//			map.put("call_list_text", call_list.GetListTime(i));			
			mListtem.add(map);
		}					
		return mListtem;
	}
	
	@Override  
	protected void onDestroy() {
		Log.i(TAG,"onDestroy");		
	    super.onDestroy();  
	    unregisterReceiver(closeThisActivity);
    }      
}
